<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="名称" prop="classifyName">
        <el-input v-model="queryParams.classifyName" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['wb:wbYearClassify:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['wb:wbClassify:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['wb:wbClassify:remove']">删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['wb:wbClassify:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table row-key="rowKey" :default-expand-all="isExpandAll" v-loading="loading" :data="wbYearClassify">
      <el-table-column label="名称" prop="classifyName">
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort">
      </el-table-column>
      <!-- <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="{row}">
          {{ row.status == 0 ? '正常' : '停用' }}
        </template>
</el-table-column> -->
      <el-table-column label="描述" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleAddItem(scope.row)"
            v-hasPermi="['wb:wbYearClassify:add']" v-if="scope.row.tier !== 3">新增</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['wb:wbYearClassify:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['wb:wbYearClassify:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改维保类目列对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="16" v-if="!isAdd">
            <el-form-item label="目录等级" prop="tier">
              <el-select v-model="form.tier" @change="tierChange" disabled>
                <el-option v-for="item, index in classTierOption" :key="index" :label="item.name"
                  :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="上级菜单" prop="parentId" v-if="classShow">
              <el-input v-model="form.parentName" size="normal" clearable :disabled="!isAdd"></el-input>
              <!-- <div v-if="tier == 3">
                <el-cascader v-model="form.parentId" :options="classOptions" :disabled="!isAdd"
                  :props="{ value: 'parentId', label: 'classifyName' }" clearable @change="change"></el-cascader>
              </div>
              <div v-else>
                <el-cascader v-model="form.parentId" :options="classOptions2" :disabled="!isAdd"
                  :props="{ value: 'parentId', label: 'classifyName' }" clearable @change="change"></el-cascader>
              </div> -->
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="名称" prop="classifyName">
              <el-input v-model="form.classifyName" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="显示顺序" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入显示顺序" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="16">
            <el-form-item label="状态" prop="status">
              <el-radio v-model="form.status" :label="0">正常</el-radio>
              <el-radio v-model="form.status" :label="1">停用</el-radio>
            </el-form-item>
          </el-col> -->
          <el-col :span="16" v-if="isSecondary">
            <el-form-item label="标准要求" prop="standardRequest">
              <el-input v-model="form.standardRequest" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="描述" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getYearClassify, setYearClassify, updataYearClassify, deleteYearClassify, getYearClassifyDetail } from '@/api/wb/wbYearClassify'
export default {
  name: "WbYearClassify",
  data() {
    return {
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维保类目列表格数据
      wbYearClassify: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tier: null,
        classifyName: null,
        sort: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tier: [{ required: true, message: '内容不能为空', trigger: 'blur' },],
        classifyName: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
        parentId: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
      },
      classifyOptions: [],
      classTierOption: [{
        id: 1,
        name: '一级菜单'
      }, {
        id: 2,
        name: '二级菜单'
      }, {
        id: 3,
        name: '三级菜单'
      }],
      classShow: false,
      classOptions: [],
      isSecondary: false,
      isAdd: true,
      tier: null,
      classOptions2: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 查询维保类目列列表 */
    getList() {
      this.loading = true;
      getYearClassify(this.queryParams).then(response => {
        if (response.code == 200) {
          this.loading = false;
          this.total = response.total;
          // this.wbYearClassify = JSON.parse(JSON.stringify(response.rows));
          this.wbYearClassify = response.rows
          this.wbYearClassify.forEach((item, index) => {
            item.rowKey = item.id
            if (item.wbClassifyYearContentVos?.length) {
              item.children = item.wbClassifyYearContentVos
              item.wbClassifyYearContentVos.forEach((i, ind) => {
                i.rowKey = `${i.classifyYearContentId}${ind}`
                // i.id = i.classifyYearContentId
                if (i.wbClassifyYearContentThreeVos?.length) {
                  i.children = i.wbClassifyYearContentThreeVos
                  i.children.forEach((element, inde) => {
                    element.rowKey = `${element.classifyContentId}${ind}${inde}`
                    element.id = element.classifyContentId
                  })
                } else {
                  i.children = null
                }
              })
            } else {
              item.children = null
            }
          })
          this.loading = false;

          this.getClassOptions(response.rows)
        }
        this.loading = false;

      });
    },
    /**
     * 根据获取的分页数据生成父级菜单列表
     */
    getClassOptions(data) {
      this.classOptions = [...data]
      this.classOptions2 = [...data]
      if (this.classOptions.length) {
        this.classOptions.forEach(item => {
          if (item.wbClassifyYearContentVos?.length) {
            item.children = [...item.wbClassifyYearContentVos]
            item.children.forEach(item => {
              item.id = item.classifyYearContentId
            })
          } else {
            item.children = null
          }
        })
      }
    },
    change(value) {
      console.log(value, 888);
      // this.form.parentId = value[value.length - 1] + ''
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tier: null,
        classifyName: null,
        sort: null,
        status: 0,
        remark: null,
        standardRequest: null,
        // radio: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维保模版";
      this.isAdd = true
      this.classShow = false
      this.isSecondary = false
      this.form.tier = 1
    },
    handleAddItem(row) {
      this.reset();
      this.open = true;
      this.title = "添加维保模版";
      this.isAdd = false
      this.classShow = true
      this.isSecondary = false
      console.log(888, row);
      switch (row.tier) {
        case 1:
          this.form.tier = 2
          this.form.parentId = row.id
          this.form.parentName = row.classifyName
          this.tier = 2
          break;
        case 2:
          this.form.tier = 3
          this.form.parentId = row.id
          this.form.parentName = row.classifyName
          this.tier = 3
          this.isSecondary = true
          break;
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.loading = false;
      const id = row.id || this.ids
      // getYearClassifyDetail(id).then(response => {
      this.form = { ...row };
      if (row.tier == 2) {
        this.classOptions.forEach(item => {
          if (item.id == row.classifyYearId) {
            this.form.parentName = item.classifyName
          }
        })
      } else if (row.tier == 3) {
        this.classOptions.forEach(item => {
          item.children?.forEach(i => {
            if (i.id == row.classifyYearContentId) {
              this.form.parentName = i.classifyName
            }
          })
        })
      }
      this.open = true;
      this.isAdd = false
      this.title = "修改维保模版";
      this.tierChange(this.form.tier)
      if (this.form.tier == 2) {
        this.form.parentId = this.form.classifyYearId
      } else if (this.form.tier == 3) {
        this.form.parentId = this.form.classifyYearContentId
      }
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updataYearClassify(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            setYearClassify(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let data = {}
      if (row.tier == 1) {
        data = { oneRankIds: [row.id] }
      } else if (row.tier == 2) {
        data = { twoRankIds: [row.classifyYearContentId] }
      } else if (row.tier == 3) {
        data = { threeRankIds: [row.classifyContentId] }
      }
      this.$modal.confirm('是否确认删除该条数据？').then(function () {
        return deleteYearClassify(data);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wb/wbClassify/export', {
        ...this.queryParams
      }, `wbClassify_${new Date().getTime()}.xlsx`)
    },
    tierChange(value) {
      if (value == 1) {
        this.classShow = false
        this.isSecondary = false
      } else if (value == 2) {
        this.isSecondary = false
        this.classShow = true
      } else if (value == 3) {
        this.isSecondary = true
        this.classShow = true
      }
      this.loading = false;

      this.tier = value
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.menuId,
        label: node.menuName,
        children: node.children
      };
    },
  }
};
</script>
